App.controller('lineController', ['$rootScope', '$scope', 'Page', function($rootScope, $scope, Page) {

	$scope.lineGrid = {
		 column_defs: [
	        {
	            target: 7,
	            buttons: [
	                {
	                    name: '站点规划',
	                    class: 'btn purple',
	                    icon: 'fa-cog',
	                    event: function(row) {
                            $scope.lineStops.showStops(row);
	                    }
	                }
	            ]
	        }
    	],
		addPlan: function(row) {
            $scope.plan = {
            	lineId: row.lineId,
                discounts: [],
                refund: {}
            };
            $scope.planAddFlag = true;
            $scope.linePlanModal.show();
		},
		addPlanDiscount: function() {
            $scope.plan.discounts.push({});
		},
        updatePlan: function(row, ele) {
            $scope.plan = null;
		 	var departTime = $(ele).text();
		 	$(row.plans).each(function(i, plan) {
				if (departTime == plan.departTime) {
                    $scope.plan = plan;
					return false;
				}
			});
		 	if ($scope.plan) {
                Page.ajaxGet('linePlan/detail.do', {planId: $scope.plan.planId}, function(resp) {
                    $scope.plan.lineId = row.lineId;
                    $scope.plan.discounts = resp.data.discounts;
                    $scope.plan.refund = resp.data.refund;
                    $scope.planAddFlag = false;
                    var arr = [];
                    $(resp.data.schedules).each(function(i, schedule) {
                        arr.push(schedule.date);
                    });
                    $scope.plan.schedules = arr;
                    $scope.linePlanModal.show();
                });
			}
		},
		savePlan: function() {
            var dates = $scope.calendars.getSelected();
            if (dates.length == 0) {
                Page.notice('请选择发车日期!');
                return;
            }
            var reg=/^((20|21|22|23|[0-1]\d)\:[0-5][0-9])?$/;
            if (!reg.test($scope.plan.departTime)) {
                Page.notice('发车时间格式不正确!');
                return;
            }
            if($scope.plan.refund.refundId == undefined || $scope.plan.refund.refundId == "") {
                Page.notice("请选择退款方案!");
                return;
            }
            $scope.plan.schedules = dates.join(',');
		 	if ($scope.planAddFlag) {
                $scope.lineGrid.addOrUpdatePlan();
			} else {
                Page.ajaxPost('linePlan/beforeUpdate.do', $scope.plan, function (resp) {
                	if (resp.data.validate == '1') {
                        $scope.lineGrid.addOrUpdatePlan();
					} else {
                		Page.confirm(resp.data.validateMsg, function() {
                            $scope.lineGrid.addOrUpdatePlan();
						});
					}
                });
			}
		},
		addOrUpdatePlan: function() {
            Page.ajaxPost($scope.planAddFlag ? 'linePlan/add.do' : 'linePlan/update.do', $scope.plan, function (resp) {
                Page.notice('方案保存成功!');
                $scope.lineGrid.query();
                $scope.linePlanModal.hide();
            });
		}
	};

    Page.ajaxGet('discount/query.do', null, function(resp) {
        $scope.discounts = resp.data.rows;
    });
    Page.ajaxGet('refund/query.do', null, function(resp) {
        $scope.refunds = resp.data.rows;
    });

    // 以下是发车日期设置
    $scope.initCalendar = function() {
        $scope.calendars.showHalfYear();
        $scope.calendars.disable($scope.disableDates);
        $scope.calendars.select($scope.plan.schedules || []);
        if (!$scope.workdays) {
            $scope.workdays = [];
            outer: for (var i = 0, dates = $scope.calendars.getEnableDates(); i < dates.length; i++) {
                for (var j = 0; j < $scope.holidays.length; j++) {
                    if (dates[i] == $scope.holidays[j]) {
                    	continue outer;
					}
                }
                $scope.workdays.push(dates[i])
            }
            // console.log($scope.workdays);
		}
	}
	$scope.setWorkdayDepart = function() {
        $scope.calendars.select($scope.workdays);
	}
    $scope.setHolidayDepart = function() {
        $scope.calendars.select($scope.holidays);
    }
    // 日历
    $scope.$on('$viewContentLoaded', function() {
    	var today = new Date(), end = new Date();
        end.setMonth(end.getMonth() + 6);
        end.setDate(1);
        Page.ajaxGet('holiday/query.do', {startDate: today.Format("yyyy-MM-dd"), endDate: end.Format("yyyy-MM-dd")}, function(resp) {
            var dates = [];
            $(resp.data).each(function(i, data) {
                dates.push(data.holidayDate.substr(0, 10));
            });
            // console.log(dates)
            $scope.holidays = dates;
        });

		$scope.disableDates = [];
		var date = new Date();
		for (var i = 0, today = date.getDate(); i < today; i++) {
			date.setDate(i + 1);
			$scope.disableDates.push(date.Format("yyyy-MM-dd"));
		}
		// console.log($scope.disableDates)
    });

}]);

App.filter('plans', [function() {
    return function (plans) {
		var html = '';
    	$(plans).each(function(index, plan) {
    		html += '<a class="plan-status plan-status-' + plan.planStatus + '" href="javascript:;" ed-click="updatePlan">' + plan.departTime + '</a>'
		});
        html += '<i class="fa fa fa-plus" style="color: #3598dc;cursor: pointer;" ed-click="addPlan"></i>';
        return html;
    };
}]);